//
// Created by Jeffrey Wei on 2022/11/2.
// 默认的实现,已实现部分自身协议属性以及

import Foundation
import Moya
import RxSwift

/// 自身新增协议属性的默认实现
public extension NetworkTargetType {
    static var loggerOptions: NetworkLoggerPlugin.Configuration.LogOptions {
        .verbose
    }

    var accessToken: (type: AuthorizationType, value: String)? {
        nil
    }

    var parameters: [String: Any]? {
        nil
    }

    func handleError(error _: NetworkError) {
    }
}

/// AccessTokenAuthorizable的默认实现
public extension NetworkTargetType {
    var authorizationType: AuthorizationType? {
        accessToken?.type
    }
}

/// 父类协议TargetType的默认实现
public extension NetworkTargetType {
    var method: Moya.Method {
        switch requestMode {
        case .postJson, .postForm, .uploadMultipart:
            return .post
        case .get:
            return .get
        }
    }

    var task: Task {
        switch requestMode {
        case let .postJson(parameters):
            return .requestParameters(parameters: parameters ?? [String: Any](), encoding: JSONEncoding.default)
        case let .postForm(parameters):
            return .requestParameters(parameters: parameters ?? [String: Any](), encoding: URLEncoding.default)
        case let .get(parameters):
            return .requestParameters(parameters: parameters ?? [String: Any](), encoding: URLEncoding.default)
        case let .uploadMultipart(data):
            return .uploadMultipart(data)
        }
    }

    var validationType: ValidationType {
        .successAndRedirectCodes
    }
}
